﻿Imports System.Windows.Forms

Public Class EditorTabManager

    Public Property TabClicked As TabPage

    Private _projectfiles As New Dictionary(Of String, Project.ProjectFile)
    Private _tabcontrol As TabControl
    Public Sub New(ByRef tabcontrol As TabControl)
        _tabcontrol = tabcontrol

        If Globals.UISettings.GetTrait(Of Boolean)("FlatTabs", True) Then
            _tabcontrol.Appearance = TabAppearance.FlatButtons
        Else
            _tabcontrol.Appearance = TabAppearance.Normal
        End If

    End Sub

    Private _selectedtabprojectfile As Project.ProjectFile
    Private _selectedtabname As String = ""
    Public Function AddTab(ByRef name As String, ByRef projecfile As Project.ProjectFile) As EditorTabPage
        Dim tab As TabPage
        If _projectfiles.ContainsKey(name) Then
            ' Select the page
            tab = _tabcontrol.TabPages(name)
        Else
            _tabcontrol.TabPages.Add(New EditorTabPage(name, projecfile))
            tab = _tabcontrol.TabPages(_tabcontrol.TabPages.Count - 1)
            tab.Name = name
            tab.Text = name
            tab.Controls.Add(New ScintillaEditor)
            tab.Controls.Item(tab.Controls.Count - 1).Text = projecfile.FileContents
            _projectfiles.Add(name, projecfile)
        End If

        _selectedtabname = name
        _selectedtabprojectfile = projecfile

        _tabcontrol.SelectedTab = tab

        Return CType(tab, EditorTabPage)
    End Function

    Public Sub SaveCurrentTab()
        If Not IsNothing(CurrentTab) Then
            CurrentTab.SaveFile()
        End If
    End Sub

    Public Sub SaveAsTab()
        Throw New NotImplementedException()
        ' CurrentTab.ShowSaveFileDialog()
    End Sub

    Public Sub SaveAllTabs()
        For i As Integer = 0 To _tabcontrol.TabCount - 1
            Dim tab As EditorTabPage = CType(_tabcontrol.TabPages(i), EditorTabPage)
            tab.SaveFile()
        Next
    End Sub

    Public Sub CloseTab(ByRef name As String)
        Dim tab = _tabcontrol.TabPages(name)
        CloseTab(tab)
    End Sub

    Public Sub CloseTab(ByRef tab As TabPage)
        If Not IsNothing(tab) Then
            If CurrentTab.ShouldCloseTab() Then
                _projectfiles.Remove(tab.Name)
                _tabcontrol.TabPages.Remove(tab)
            End If
        End If
    End Sub

    Public Sub CloseCurrentTab()
        If _tabcontrol.SelectedIndex >= 0 And
            _tabcontrol.SelectedIndex < _tabcontrol.TabPages.Count Then
            CloseTab(CurrentTab)
            '_tabcontrol.TabPages.RemoveAt(_tabcontrol.SelectedIndex)
        End If
    End Sub

    Public Sub CloseOthers(ByRef tab As TabPage)
        ' Close in reverse order
        For i As Integer = _tabcontrol.TabCount - 1 To 0 Step -1

            Dim tabtoclose = _tabcontrol.TabPages(i)
            If tabtoclose.Name <> tab.Name Then
                CloseTab(tabtoclose)
            End If
        Next
    End Sub

    Public Sub CloseAll()
        _tabcontrol.TabPages.Clear()
    End Sub

    Public Sub ChangeThemes(ByRef themepath As String)
        For Each tabpage In _tabcontrol.TabPages
            Dim editortab = CType(tabpage, EditorTabPage)
            editortab.ScintillaEditor.ChangeTheme(themepath)
        Next
    End Sub

    ReadOnly Property CurrentTab As EditorTabPage
        Get
            If _tabcontrol.SelectedIndex >= 0 And
                _tabcontrol.SelectedIndex < _tabcontrol.TabCount Then
                Return CType(_tabcontrol.TabPages(_tabcontrol.SelectedIndex), EditorTabPage)
            Else
                Return Nothing
            End If
        End Get
    End Property

    ReadOnly Property CurrentTabProjectFile As Project.ProjectFile
        Get
            Return CurrentTab.ProjectFile
        End Get
    End Property

    ReadOnly Property SelectedTabName As String
        Get
            Return _selectedtabname
        End Get
    End Property

    ReadOnly Property SelectedTab As EditorTabPage
        Get
            Dim selected_tab As EditorTabPage
            selected_tab = CType(_tabcontrol.TabPages(_selectedtabname), EditorTabPage)

            Return selected_tab
        End Get
    End Property

    ReadOnly Property SelectedTabProjectFile As Project.ProjectFile
        Get
            Return _selectedtabprojectfile
        End Get
    End Property

    ReadOnly Property TabControl As TabControl
        Get
            Return _tabcontrol
        End Get
    End Property

    ReadOnly Property TabPages As TabControl.TabPageCollection
        Get
            Return _tabcontrol.TabPages
        End Get
    End Property

    ReadOnly Property TabCount As Integer
        Get
            Return _tabcontrol.TabCount
        End Get
    End Property

End Class
